package com.hgyc.mom.mes.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.common.enums.CommonStatus;
import com.hgyc.mom.common.enums.SysYesNoEnum;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.mes.entity.MesProcess;
import com.hgyc.mom.mes.vo.MesProcessParam;
import com.hgyc.mom.mes.service.MesProcessService;
import com.hgyc.mom.mes.vo.MesProcessVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 生产工序Controller
 *
 * @author fwj
 * @since 2025-06-30
 */
@Tag(name = "生产工序控制器", description = "生产工序控制器")
@RestController
@RequestMapping("/mes/process")
public class MesProcessController {
    @Autowired
    private MesProcessService mesProcessService;

    @Operation(summary = "查询生产工序列表", description = "查询生产工序列表")
    @PostMapping("/pageList")
    public Result<IPage<MesProcess>> pageList(@Parameter(description = "查询参数") @RequestBody MesProcessParam mesProcessParam) {
        Page<MesProcess> page = new Page<>(mesProcessParam.getPage(), mesProcessParam.getPageSize());
        MesProcess mesProcess = new MesProcess();
        BeanUtils.copyProperties(mesProcessParam, mesProcess);
        QueryWrapper<MesProcess> wrapper = QueryWrapperUtil.build(mesProcess);
        IPage<MesProcess> list = mesProcessService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询生产工序列表", description = "查询生产工序列表")
    @GetMapping("/list")
    public Result<List<MesProcess>> list() {
        LambdaQueryWrapper<MesProcess> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MesProcess::getEnableFlag, CommonStatus.ENABLE.getValue());
        return Result.success(mesProcessService.list(wrapper));
    }

    @Operation(summary = "获取生产工序详细信息", description = "获取生产工序详细信息")
    @GetMapping(value = "/{id}")
    public Result<?> getInfo(@Parameter(description = "生产工序ID") @PathVariable("id") Long id) {
        return Result.success(mesProcessService.getById(id));
    }
    @Operation(summary = "新增生产工序", description = "新增生产工序")
    @PostMapping
    public Result<?> save(@Parameter(description = "生产工序") @RequestBody MesProcessVO mesProcessVO) {
        mesProcessService.saveProcess(mesProcessVO);
        return Result.success();
    }

    @Operation(summary = "修改生产工序", description = "修改生产工序")
    @PutMapping
    public Result<?> update(@Parameter(description = "生产工序") @RequestBody MesProcessVO mesProcessVO) {
        mesProcessService.updateProcess(mesProcessVO);
        return Result.success();
    }

    @Operation(summary = "删除生产工序", description = "删除生产工序")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(mesProcessService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesProcess> util = new ExcelUtil<>(MesProcess.class);
        util.importTemplateExcel(response, "生产工序");
    }

    @Operation(summary = "导出生产工序列表", description = "导出生产工序列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProcessParam mesProcessParam) {
        MesProcess mesProcess = new MesProcess();
        BeanUtils.copyProperties(mesProcessParam, mesProcess);
        QueryWrapper<MesProcess> wrapper = QueryWrapperUtil.build(mesProcess);
        List<MesProcess> list = mesProcessService.list(wrapper);
        ExcelUtil<MesProcess> util = new ExcelUtil<>(MesProcess.class);
        util.exportExcel(response, list, "生产工序数据");
    }

    @Operation(summary = "导入生产工序列表", description = "导入生产工序列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesProcess> util = new ExcelUtil<>(MesProcess.class);
        List<MesProcess> mesProcessList = util.importExcel(file.getInputStream());
        String message = mesProcessService.importData(mesProcessList, updateSupport);
        return Result.success(message);
    }
}